<?php
/* 
 * ebiz front
 * 
 * Promotion Dao
 * 
 * @author allen
 * 
 */

require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class PromotionDao extends BaseDao 
{
    private $tableName = "promotions";
	     
    public function __construct($dbUtil){
    	parent::__construct($dbUtil);        
    }
    
    public function getPromotionInfo(){
        $param=array();
        $sql="select * from $this->tableName where status = 1 and promotion_type = 1 and is_delete = 0";
        return $this->dbUtil->SelectData($sql,$param);
    }

    /**
     * for promotion implement
     */
    public function getPromotionByCouponCode($code){
        $param = array(strtolower($code));
        //$sql = "select * from $this->tableName where lower(code) = ? and status = 1 and is_delete = 0 and date_from <= now() and date_to >= now() ";
        $sql = "select p.*,pc.promotion_condition_type as promotion_condition_type,pc.code_value as code_value ,pc.condition_value as condition_value from $this->tableName as p 
               left outer join promotion_conditions as pc on pc.promotion_id = p.id and pc.is_delete = 0 and pc.promotion_condition_type = 7 
               where p.status = 1 and p.is_delete = 0 and p.date_from <= now() and p.date_to >= now() and pc.code_value = ? ";
        $result = $this->dbUtil->SelectData($sql,$param);
        if(empty($result)){
            return null;
        }
        return $result[0];
    }
    
    public function getAutoPromotions(){
        $sql = "select * from $this->tableName where auto_apply = 1 and status = 1 and is_delete = 0 and date_from <= now() and date_to >= now() ORDER BY priority ASC ";
        $result = $this->dbUtil->SelectData($sql,array());
        if(empty($result)){
            return null;
        }
        return $result;
    }
    
    /**
     * for promotion implement processFreeShipping
     */
    public function getPromotionFreeShippingInfo(){
        $param = array();
        $sql = "select * from $this->tableName p 
        join promotion_conditions pc on p.id = pc. promotion_id 
        where p.is_delete = 0 and p.status = 1 and promotion_type = 1";
        return $this->dbUtil->SelectData($sql,$param);
    }
    
    /**
     * update promotion count of used
     * @param $promotionId
     */
    public function updateUsedCount($promotionId){
        $param = array($promotionId);
        $sql = "update $this->tableName set `used` = `used` + 1 where id = ? ";
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }
    
    /**
     * 积分兑换抵用券显示promotion表中信息
     */
    public function getExchangePromotion(){
    	$param = array();
    	$sql = "select * from $this->tableName 
    	where is_exchange_point=1 and exchange_start_date <= now() and exchange_end_date >= now() and is_delete=0 ";
    	return $this->dbUtil->SelectData($sql, $param);
    }
    
    /**
     * 根据id查找promotion
     */
    public function getPromotionById($id){
    	$param = array($id);
    	$sql = "select * from $this->tableName 
    	where id=? and exchange_start_date <= now() and exchange_end_date >= now() and is_delete=0 ";
    	return $this->dbUtil->SelectData($sql, $param);
    }
    
    /**
     * 根据id查找promotion信息,用于优化券详情
     */
    public function getPromotionInfoById($id){
    	$param = array($id);
    	$sql = "select * from $this->tableName where id=? and is_delete=0 ";
    	return $this->dbUtil->SelectData($sql, $param);
    }
    
}

